package com.kpiJavaTraining.bogdan.module2;

import java.util.ArrayList;
import java.util.Random;

public class LearnQuestionsByTopic {
	private ArrayList<LearnQuestion> learnQuestionsByTopic;
	private int summaryScore;
	private int countOfAsking;
	private boolean isLearning;
	private  int averageDifficulty;
	private double  levelOfGrasp;
	//good method
	
	private void update(){
		this.determineSummaryScore();
		this.calculateCountOfAsking();
		this.determineLearning();
		this.determineLevelOfGrasp();	
	}
	
	public LearnQuestionsByTopic(){
		this.learnQuestionsByTopic=new ArrayList<LearnQuestion>();
		
	}
	
	public void add(LearnQuestion e){
		this.learnQuestionsByTopic.add(e);
	}
	
	public double getLevelOfGrasp(){
		this.determineLevelOfGrasp();
		return this.levelOfGrasp;
	}
	
	public LearnQuestion getLearnQuestion(int index){
		return this.learnQuestionsByTopic.get(index);
	}
	
	public int size(){
		return this.learnQuestionsByTopic.size();
	}
	
	public boolean  getLearning(){
		determineLearning();
		return this.isLearning;
	}
	
	private void determineLearning(){
		boolean temp=true;
		for(LearnQuestion e:this.learnQuestionsByTopic){
			temp=temp && e.getLearned();
		}
		this.isLearning=temp;
	}
	
	/**
	 * summarizes the number of asking questions
	 */
	private void calculateCountOfAsking(){
		int temp=0;
		for(LearnQuestion e:this.learnQuestionsByTopic){
			temp+=e.getCountOfAsking();
		}
		this.countOfAsking=temp;
	}
	
	public   void  determineAvarageDifficulty(){
		int temp=0;
		for(LearnQuestion e:this.learnQuestionsByTopic){
			temp+=e.getDifficulty();
		}
		this.averageDifficulty= temp/this.learnQuestionsByTopic.size();
	}
	
	/**
	 * 
	 * @return  average difficulty
	 */
	public int getAverageDifficulty(){
		return this.averageDifficulty;
	}
	
	/**
	 * This method update summary score of questions in this topic 
	 */
	private void determineSummaryScore(){
		for(LearnQuestion e: this.learnQuestionsByTopic){
			this.summaryScore+=e.getScore();
		}
	}
	/**
	 * This method checks whether each question is asked with the topic.
	 * @return 
	 */
	public boolean isAllQuestionAsking(){
		boolean allLearning=true;
		for(LearnQuestion e: this.learnQuestionsByTopic){
			allLearning=allLearning &&(e.getCountOfAsking()!=0);
		}
		
		return allLearning;
	}
	
	
	
	
	
	
	
	
	

	private LearnQuestion findQuestionWithMinLevel(){
		int r;
		do{
			Random generator= new Random();
			r= Math.abs(generator.nextInt()%this.learnQuestionsByTopic.size());
		}
		while(this.learnQuestionsByTopic.get(r).getGrasp()>=this.levelOfGrasp &&(this.learnQuestionsByTopic.get(r).getLearned()));		
		return  this.learnQuestionsByTopic.get(r);
	}
	
	
	
	// so child logic 
	private void determineLevelOfGrasp(){
		double temp=0;
		for(LearnQuestion e:this.learnQuestionsByTopic){
			temp+=e.getGrasp();
		}
		this.levelOfGrasp=temp/this.size();
	}
		
	public void ask(){
		determineLevelOfGrasp();
		
		LearnQuestion temp= this.findQuestionWithMinLevel();
		new Printer().println(temp);
		temp.answer(new Reader().readLn());
	/*	boolean ask=false;
	
		for(LearnQuestion e:this.learnQuestionsByTopic){
			
			if(!e.getLearned()&& e.getDifficulty()<this.levelOfGrasp &&!ask){
				new Printer().println(e);
				e.answer(new Reader().readLn());
				ask=true;
			}
		}
		if(!ask){
			Random generator= new Random();
			int position= Math.abs(generator.nextInt()%this.size());
			new Printer().println(this.learnQuestionsByTopic.get(position));
			this.learnQuestionsByTopic.get(position).answer(new Reader().readLn());
		}
		*/
		this.update();
	}
	
	public void askRandom(){
		for(LearnQuestion e:this.learnQuestionsByTopic){
			if(e.getCountOfAsking()==0){
				new Printer().println(e);
				e.answer(new Reader().readLn());
				break;
			}
			
		}
		this.update();
	}
	
	
	
}
